import os

import wx
import networkx
import matplotlib

from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import \
    FigureCanvasWxAgg as FigCanvas
import numpy as np

import pylab


class GraphPanel(wx.Panel):
    def __init__(self, task, draw, parent=None, id=-1):
        wx.Panel.__init__(self, parent, id)
        self.task = task
        self.draw = draw
        self.sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.init_plot()
        #self.figure = Figure()
        #self.canvas = FigCanvas(self, -1, self.figure)
        self.sizer.Add(self.canvas, 1, wx.EXPAND)
        self.SetSizer(self.sizer)

        self.G = networkx.XGraph()

    def init_plot(self):
        self.dpi = 100
        self.fig = Figure((3.0, 3.0), dpi=self.dpi)
        self.canvas = FigCanvas(self, -1, self.fig)

    def draw_graph(self):
        try:
            self.axes.clear()
        except:
            pass
        self.axes = self.fig.add_subplot(111)
        self.G = self.task.G_pp
        networkx.draw(self.G, ax=self.axes, node_size = self.draw.node_size)
        self.canvas.draw()


class DrawSettingPanel(wx.Panel):
    def __init__(self, task, draw, parent=None, id=-1):
        self.task = task
        self.draw = draw
        wx.Panel.__init__(self, parent, id)
        self.sizer = wx.BoxSizer(wx.VERTICAL)

        box = wx.BoxSizer(wx.HORIZONTAL)
        label = wx.StaticText(self, -1, '  Node Size')
        box.Add(label, 2, wx.EXPAND)

        self.sc = wx.SpinCtrl(self, -1, "")
        self.sc.SetRange(1,100)
        self.sc.SetValue(5)

        box.Add(self.sc, 3, wx.EXPAND|wx.ALIGN_RIGHT)
        self.sizer.Add(box, 0, wx.ALIGN_LEFT|wx.EXPAND)

        box = wx.BoxSizer(wx.HORIZONTAL)

        label = wx.StaticText(self, -1, '  Node Color')
        box.Add(label, 2, wx.EXPAND)

        self.color_ch = wx.Choice(self, -1, choices=['black', 'red', 'green','blue'])
        self.Bind(wx.EVT_CHOICE, self.OnColorChange, self.color_ch)
        box.Add(self.color_ch, 3, wx.EXPAND|wx.ALIGN_RIGHT)

        self.sizer.Add(box, 0, wx.ALIGN_LEFT|wx.EXPAND)

        box = wx.BoxSizer(wx.HORIZONTAL)
        label = wx.StaticText(self, -1, '  Node Shape')
        box.Add(label, 2, wx.EXPAND)
        self.shape_ch = wx.Choice(self, -1, choices=['o'])
        self.Bind(wx.EVT_CHOICE, self.OnShapeChange, self.shape_ch)
        box.Add(self.shape_ch, 3, wx.EXPAND)
        self.sizer.Add(box, 0, wx.ALIGN_LEFT|wx.EXPAND)

        #box = wx.BoxSizer(wx.HORIZONTAL)
        #label = wx.StaticText(self, -1, '  Edge Color')
        #box.Add(label, 2, wx.EXPAND)
        #self.color_ch

        box = wx.BoxSizer(wx.HORIZONTAL)
        label = wx.StaticText(self, -1, '  Layout')
        box.Add(label, 2, wx.EXPAND)
        self.layout_ch = wx.Choice(self, -1, choices = ['spring'])
        self.Bind(wx.EVT_CHOICE, self.OnLayoutChange, self.layout_ch)
        box.Add(self.layout_ch, 3, wx.EXPAND)
        self.sizer.Add(box, 0, wx.ALIGN_LEFT|wx.EXPAND)

        button = wx.Button(self, -1, 'Update')
        self.sizer.Add(button, 0, wx.LEFT)
        self.Bind(wx.EVT_BUTTON, self.OnUpdate, button)

        self.SetSizer(self.sizer)

    def OnColorChange(self, e):
        self.draw.node_color = e.GetString()

    def OnLayoutChange(self, e):
        self.draw.layout = e.GetString()

    def OnUpdate(self, e):
        self.draw.node_size = self.sc.GetValue()
        #self.draw.node_color = self.color_ch.GetString()
        #self.draw.layout = self.layout_ch.GetString()
        #print str(dir(self.draw))
        graph_panel = self.GetParent()

    def OnShapeChange(self, e):
        self.draw.node_shape = e.GetString()

class InputSettingPanel(wx.Panel):
    def __init__(self, task, parent=None, id=-1):
        wx.Panel.__init__(self, parent, id)
        self.task = task
        self.protein_grid = None
        self.orth_grid = None

        sizer = wx.BoxSizer(wx.VERTICAL)

        label = wx.StaticText(self, -1, '  Choose Methods')
        sizer.Add(label, 0)
        cb1 = wx.CheckBox(self, -1, "Phylogentic Profile")
        self.Bind(wx.EVT_CHECKBOX, self.SelectPP, cb1)
        cb2 = wx.CheckBox(self, -1, "Gene Neighbor")
        self.Bind(wx.EVT_CHECKBOX, self.SelectGN, cb1)
        cb1.SetValue(True)
        cb2.SetValue(True)

        box = wx.BoxSizer(wx.VERTICAL)
        box.Add(cb1, 0)
        box.Add(cb2, 0)

        sizer.Add(box, 0, wx.EXPAND)

        spacer = wx.Panel(self, -1, (20, 20))
        sizer.Add(spacer, 0)

        label = wx.StaticText(self, -1, '  Protein File')
        sizer.Add(label, 0, wx.EXPAND)

        box = wx.BoxSizer(wx.HORIZONTAL)

        self.protein_text = wx.TextCtrl(self, -1, '')
        box.Add(self.protein_text, 5, wx.ALIGN_LEFT)

        button = wx.Button(self, -1, '...')
        self.Bind(wx.EVT_BUTTON, self.OnProteinFileOpen, button)
        box.Add(button, 1, wx.ALIGN_RIGHT)

        sizer.Add(box, 0, wx.EXPAND)

        label = wx.StaticText(self, -1, '  Ortholog File')
        sizer.Add(label, 0, wx.ALIGN_LEFT)

        box = wx.BoxSizer(wx.HORIZONTAL)

        self.orth_text = wx.TextCtrl(self, -1, '')
        box.Add(self.orth_text, 5, wx.ALIGN_LEFT)

        button = wx.Button(self, -1, '...')
        self.Bind(wx.EVT_BUTTON, self.OnOrthFileOpen, button)
        box.Add(button, 1, wx.ALIGN_RIGHT)

        sizer.Add(box, 0, wx.EXPAND)

        button = wx.Button(self, -1, 'Load Data')
        self.Bind(wx.EVT_BUTTON, self.OnLoadData, button)
        sizer.Add(button, 0)

        spacer = wx.Panel(self, -1, (20, 20))
        sizer.Add(spacer, 0)

        label = wx.StaticText(self, -1, '  Select Taxon (after data loaded)')
        sizer.Add(label, 0, wx.ALIGN_LEFT)

        self.ch = wx.Choice(self, -1, (100, 50), choices = [])
        self.ch.Disable()
        self.Bind(wx.EVT_CHOICE, self.EvtChoice, self.ch)
        sizer.Add(self.ch, 0, wx.ALIGN_LEFT)


        spacer = wx.Panel(self, -1)
        sizer.Add(spacer, 1, wx.EXPAND)

        self.SetSizer(sizer)

    def OnProteinFileOpen(self, event):
        dlg = wx.FileDialog(
            self, message="Choose a protein information file",
            defaultDir=os.getcwd(),
            defaultFile="",
            style=wx.OPEN | wx.CHANGE_DIR
            )

        if dlg.ShowModal() == wx.ID_OK:
            # This returns a Python list of files that were selected.
            self.task.protein_file = dlg.GetPath()
            self.protein_text.SetValue(str(self.task.protein_file))

        dlg.Destroy()

    def OnOrthFileOpen(self, event):
        dlg = wx.FileDialog(
            self, message="Choose a ortholog pair file",
            defaultDir=os.getcwd(),
            defaultFile="",
            style=wx.OPEN | wx.CHANGE_DIR
            )

        if dlg.ShowModal() == wx.ID_OK:
            # This returns a Python list of files that were selected.
            self.task.orth_file = dlg.GetPath()
            self.orth_text.SetValue(str(self.task.orth_file))

        dlg.Destroy()

    def OnLoadData(self, e):
        input_nb = self.GetParent()
        up_panel = input_nb.GetParent()
        main_frame = up_panel.GetParent()

        protein_grid = main_frame.protein_grid
        protein_grid.update_data(self.task.protein_file)
        orth_grid = main_frame.orth_grid
        orth_grid.update_data(self.task.orth_file)
        self.ch.AppendItems(list(self.task.all_taxon))
        self.ch.Enable()
        self.ch.Update()

    def updata_grid(self, protein_grid=None, orth_grid=None):
        self.protein_grid = protein_grid
        self.orth_grid = orth_grid

    def EvtChoice(self, e):
        self.task.taxon_id = e.GetString()

    def SelectPP(self, e):
        if e.IsChecked():
            self.task.pp = 1
        else:
            self.task.pp = 0

    def SelectGN(self, e):
        if e.IsChecked():
            self.task.gn = 1
        else:
            self.task.gn = 0



